package org.compiere.model;

import java.io.File;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;

import org.compiere.process.DocAction;
import org.compiere.process.DocumentEngine;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;

public class MBCSFRInputFormCompany extends X_BCS_FRInputForm_Company implements DocAction{

	private static CLogger s_log = CLogger.getCLogger(MBCSFRInputFormCompany.class);

	public MBCSFRInputFormCompany(Properties ctx, int MBCSFRInputFormCompany_ID,
			String trxName) {
		super(ctx, MBCSFRInputFormCompany_ID, trxName);
		// TODO Auto-generated constructor stub
	}
	
	public MBCSFRInputFormCompany(Properties ctx, ResultSet rs, String trxName){
		super(ctx, rs, trxName);
	}

//	protected boolean afterDelete(boolean success){
//		
//		
//		
////		delete all child item
//				
////				DB.executeUpdate("delete BCS_FRInputFormline WHERE BCS_FRInputForm_Company_ID ="  +  get_ID());
////				DB.executeUpdate("delete BCS_FRInputFormcolumn WHERE BCS_FRInputForm_Company_ID ="  +  get_ID());
//				
//		
//		return true;
//		
//	}

	
	protected boolean afterDelete(boolean success){
		
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "";	

		//delete companies
		
		List<Integer> forms = new ArrayList<Integer>();
		
		sql = " SELECT BCS_FRInputForm_ID FROM BCS_FRInputForm \n " +
				   " WHERE BCS_FRInputform_Company_ID ="  +  get_ID();

			try{
				pstmt = DB.prepareStatement(sql,null);				
				rs = pstmt.executeQuery();	
				while(rs.next()){
					forms.add(rs.getInt(1));

					// too long
//					MBCSFRInputForm form = new MBCSFRInputForm(Env.getCtx(), rs.getInt(1), null);
//					if(form != null)
//						form.delete(true);
						DB.executeUpdate("delete BCS_FRInputForm WHERE BCS_FRInputForm_ID ="  +  rs.getInt(1),get_TrxName());						
				}
			}catch (SQLException e){
				s_log.log(Level.SEVERE, "MBCSFRInputFormCompany", e);
			}finally{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}			


			
			
		for(Integer form: forms){
		//---------------------------------------------
		    pstmt = null;
			rs = null;
			sql = "";	
			sql = " SELECT BCS_FRInputFormLine_ID FROM BCS_FRInputFormLine \n " +
					   " WHERE BCS_FRInputform_ID ="  +  form;

				try{
					pstmt = DB.prepareStatement(sql,null);				
					rs = pstmt.executeQuery();	
					while(rs.next()){
						
						DB.executeUpdate("delete BCS_FRInputFormLine WHERE BCS_FRInputFormLine_ID ="  +  rs.getInt(1),get_TrxName());
						DB.executeUpdate("delete BCS_FRInputFormColumn WHERE BCS_FRInputFormLine_ID ="  +  rs.getInt(1), get_TrxName());
//						MBCSFRInputFormLine line = new MBCSFRInputFormLine(Env.getCtx(), rs.getInt(1), null);
//						if(line != null)
//							line.delete(true);

//						DB.executeUpdate("delete BCS_FRInputForm WHERE BCS_FRInputForm_ID ="  +  rs.getInt(1));
					}
				}catch (SQLException e){
					s_log.log(Level.SEVERE, "MBCSFRInputForm", e);
				}finally{
					DB.close(rs, pstmt);
					rs = null; pstmt = null;
				}			
			
		}

		
			
		return true;
	}

	@Override
	public boolean processIt(String processAction) throws Exception {
//		// TODO Auto-generated method stub
//		return false;
		m_processMsg = null;
		DocumentEngine engine = new DocumentEngine (this, getDocStatus());
		return engine.processIt (processAction, getDocAction());		
	}

	/**	Process Message 			*/
	private String		m_processMsg = null;
	/**	Just Prepared Flag			*/
	private boolean		m_justPrepared = false;	
	
	@Override
	public boolean unlockIt() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean invalidateIt() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public String prepareIt() {
		
		System.out.println("prepareIT()");
		
		log.info(toString());
//		m_processMsg = ModelValidationEngine.get().fireDocValidate(this, ModelValidator.TIMING_BEFORE_COMPLETE);
//		if (m_processMsg != null)
//			return DocAction.STATUS_Invalid;
//
//		m_processMsg = ModelValidationEngine.get().fireDocValidate(this, ModelValidator.TIMING_AFTER_COMPLETE);
//		if (m_processMsg != null)
//			return DocAction.STATUS_Invalid;

		
		//	Add up Amounts
		// a.nurpiissov
		String docStatus  = getDocStatus();
		if(docStatus.equals(DocAction.STATUS_Drafted))
			return DocAction.STATUS_Approved;
		
//		m_justPrepared = true;
		if(docStatus.equals(DocAction.STATUS_Approved))
//		if (!DOCACTION_Complete.equals(getDocStatus()))
//			setDocAction(DOCACTION_Complete);
		return DocAction.STATUS_Completed;
		
		
		return null;
	}

	@Override
	public boolean approveIt() {
		System.out.println("approveIT()");
////		return false;
////		setIsApproved(true);
////		return true;
////		String s = getDocStatus();
////		if(s.equals(DocAction.ACTION_Approve))
////			setDocAction(DocAction.ACTION_Complete);
//		setIsApproved(true);
		setDocAction(DocAction.ACTION_Checked);
//		return true;//DocAction.STATUS_Approved;
		
		log.info(toString());
		setIsApproved(true);
		return true;
	}

//	private void setIsApproved(boolean b) {
//		set_ValueNoCheck (COLUMNNAME_IsApproved, Boolean.valueOf(IsApproved));
//	}

	@Override
	public boolean rejectIt() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public String completeIt() {
		System.out.println("comleteIT()");
//		return null;
//		String s = getDocStatus();
//		if(s.equals(DocAction.STATUS_Completed)){
//			setProcessed(true);
			setDocAction(DocAction.ACTION_Commited);
			return DocAction.STATUS_Commited;
//		}
		
//		return DocAction.STATUS_Approved;
	}

	@Override
	public boolean voidIt() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean closeIt() {
		setIsClosed(true);
		setProcessed(true);
		setDocAction(DocAction.ACTION_Close);
		setDocStatus(DocAction.STATUS_Closed);
		return true;
	}

	@Override
	public boolean reverseCorrectIt() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean reverseAccrualIt() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean reActivateIt() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public String getSummary() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getDocumentNo() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getDocumentInfo() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public File createPDF() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getProcessMsg() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getDoc_User_ID() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getC_Currency_ID() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public BigDecimal getApprovalAmt() {
		// TODO Auto-generated method stub
		return null;
	}
	
}
